<template>
  <div class="login-container">
    <div class="login-form-container">
      <div class="login-title">
        <h2>积分商城</h2>
        <p class="login-subtitle">欢迎登录积分商城系统</p>
      </div>

      <van-form @submit="onSubmit">
        <van-cell-group>
          <van-field
            v-model="username"
            name="username"
            placeholder="请输入用户名"
            :rules="[{ required: true, message: '请输入用户名' }]"
            left-icon="user-o"
          />
          <van-field
            v-model="password"
            type="password"
            name="password"
            placeholder="请输入密码"
            :rules="[{ required: true, message: '请输入密码' }]"
            left-icon="lock"
          />
        </van-cell-group>

        <div class="login-button">
          <van-button round block type="primary" native-type="submit" :loading="loading">登录</van-button>
        </div>
        <div class="register-link">
          <span @click="goToRegister">没有账号？立即注册</span>
        </div>
      </van-form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { showSuccessToast, showFailToast } from 'vant'
import { useUserStore } from '@/stores/user'

const router = useRouter()
const route = useRoute()
const userStore = useUserStore()

const username = ref('')
const password = ref('')
const loading = ref(false)

// 登录表单提交
const onSubmit = async () => {
  loading.value = true

  try {
    const success = await userStore.login(username.value, password.value)

    if (success) {
      showSuccessToast('登录成功')

      // 如果有重定向路径，则跳转到该路径
      const redirectPath = (route.query.redirect as string) || '/home'
      router.replace(redirectPath)
    } else {
      showFailToast('登录失败，请检查用户名和密码')
    }
  } catch (error) {
    console.error('登录异常:', error)
    showFailToast('登录异常，请稍后重试')
  } finally {
    loading.value = false
  }
}

// 跳转到注册页面
const goToRegister = () => {
  router.push('/register')
}
</script>

<style scoped lang="scss">
.login-container {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #f5f5f5;
}

.login-form-container {
  width: 400px;
  padding: 40px;
  background-color: #ffffff;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

.login-title {
  text-align: center;
  margin-bottom: 30px;

  h2 {
    font-size: 24px;
    color: #333;
    margin-bottom: 8px;
    font-weight: 600;
  }

  .login-subtitle {
    font-size: 14px;
    color: #999;
  }
}

:deep(.van-cell) {
  margin-bottom: 20px;
  padding: 14px 16px;
  background-color: #f8f8f8;
  border-radius: 4px;
}

:deep(.van-field__left-icon) {
  margin-right: 10px;
  color: #999;
}

.login-button {
  margin-top: 30px;

  .van-button {
    height: 44px;
    font-size: 16px;

    &:active {
      opacity: 0.8;
    }
  }
}

.register-link {
  margin-top: 16px;
  text-align: center;
  font-size: 14px;
  color: #1989fa;
}
</style> 